<?php
class fileAction extends commonAction{
    protected $FileManage;
    function __construct() {
        parent::__construct();
        helper('file');
        $options=array(
            
        );//公共配置参数
        $this->FileManage=new FileManage($options);//实例化TP文件管理类
    }
    
    
    //上传请求入口
    function upload(){
        if(!$this->isPost()){
            exit('非法请求');
        }
        $subaction=I('post.subaction');
        if($subaction=="litpic"){
            $this->_upLitpic();
        }elseif($subaction=="pic"){
            $this->_upPic();
        }else{
            
            $this->_upFile();
        }
    }
    
    //缩略图上传
    private function _upLitpic(){
        $this->FileManage->allowExts=array("jpg","jpeg","gif","png");//图片文件名后缀限制
        $this->FileManage->maxSize=1024*1024*200;//图片大小只允许200kb
        $this->FileManage->savePath=C('cms_attpath').'image/';//图片保存路径
        $this->FileManage->autoSub=TRUE;//多级目录保存
        $this->FileManage->subType="date";//日期方式
        $this->FileManage->dateFormat="Ym/d";//日期规则，Y年m月d日
        
        if(!$this->FileManage->upload()){
            $this->ajaxReturn(array("error"=>1,"message"=>$this->FileManage->getErrorMsg()));//上传失败，返回错误
        }else{
            $fileinfo=$this->FileManage->getUploadFileInfo();//获取上传完成后的文件信息
            $savename=trim($fileinfo[0]['savepath'],'/').'/'.$fileinfo[0]['savename'];//相对于站点根目录的文件路径
            $thumbsavename=dirname($savename).'/lt_'.basename($savename);//设置缩略图的文件名，在原文件名前加lt_前缀
            import("ORG.Util.Image");//加载图片处理类
            Image::thumb($savename,$thumbsavename,'',150,150,true);//生成缩略图
            
            $this->ajaxReturn(array("error"=>0,"url"=>__ROOT__.'/'.$thumbsavename));//返回缩略图json信息
        }
    }
    
    //处理普通图片上传请求
    private  function _upPic(){
        $this->FileManage->allowExts=array("jpg","jpeg","gif","png");//图片文件名后缀限制
        $this->FileManage->maxSize=1024*1024*200;//图片大小只允许200kb
        $this->FileManage->savePath=C('cms_attpath').'image/';//图片保存路径
        $this->FileManage->autoSub=TRUE;//多级目录保存
        $this->FileManage->subType="date";//日期方式
        $this->FileManage->dateFormat="Ym/d";//日期规则，Y年m月d日
        
        if(!$this->FileManage->upload()){
            $this->ajaxReturn(array("error"=>1,"message"=>$this->FileManage->getErrorMsg()));//上传失败，返回错误
        }else{
            $fileinfo=$this->FileManage->getUploadFileInfo();//获取上传完成后的文件信息
            $savename=trim($fileinfo[0]['savepath'],'/').'/'.$fileinfo[0]['savename'];//相对于站点根目录的文件路径
            $this->ajaxReturn(array("error"=>0,"url"=>__ROOT__.'/'.$savename));//返回图片json信息
        }
    }
    
    //普通文件上传
    private function _upFile(){
        $this->FileManage->allowExts=array("zip","rar","tar","gz","txt");//文件名后缀限制
        $this->FileManage->maxSize=1024*1024*1024*10;//图片大小只允许10m
        $this->FileManage->savePath=C('cms_attpath').'files/';//图片保存路径
        $this->FileManage->autoSub=TRUE;//多级目录保存
        $this->FileManage->subType="date";//日期方式
        $this->FileManage->dateFormat="Ym/d";//日期规则，Y年m月d日
        
        if(!$this->FileManage->upload()){
            $this->ajaxReturn(array("error"=>1,"message"=>$this->FileManage->getErrorMsg()));//上传失败，返回错误
        }else{
            $fileinfo=$this->FileManage->getUploadFileInfo();//获取上传完成后的文件信息
            $savename=trim($fileinfo[0]['savepath'],'/').'/'.$fileinfo[0]['savename'];//相对于站点根目录的文件路径
            $this->ajaxReturn(array("error"=>0,"url"=>__ROOT__.'/'.$savename));//返回图片json信息
        }
    }

    //文件列表
    function filelist(){
        $root_path=C("cms_attpath");//附件根目录
        $root_url=__ROOT__.'/'.trim($root_path);//根URL
        $path=I('path',"");//path路径参数
        $order=I('order',"NAME");//排序参数
        $listarr=$this->FileManage->GetFileList($root_path,$root_url,$path,$order);//获取文件列表数组
        $this->ajaxReturn($listarr);//返回ajax数据
    }
    
    //文件管理
    function filemanage(){
        $this->display();
    }
    //elfinder连接器
    function elconn(){
        define("IN_ADMIN","1");
        include APP_PATH."plugin/elfinder/php/connector.php";
    }
}
